/// Created with Android Studio.
/// User: 三帆
/// Date: 16/01/2019
/// Time: 11:16
/// email: sanfan.hx@alibaba-inc.com
/// target:  app首页

import 'package:flutter/material.dart';

/// import 'package:flutter/rendering.dart';
//import 'package:ivc/utils/data_utils.dart';
import '../globals.dart' as globals;


/// import 'package:ivc/views/first_page/first_page.dart';
import 'package:ivc/views/ai_page/ai_page.dart';

/// import 'package:ivc/views/collection_page/collection_page.dart';
import 'package:ivc/routers/application.dart';

/// import 'package:ivc/utils/provider.dart';

/// import 'package:ivc/widgets/index.dart';
import 'package:ivc/model/user_info.dart';
import 'factory/factory_overview.dart';
import 'local_platform/dashboard.dart';
import "factory/biz_list.dart";
import "role/factory_owner.dart";

class AppPage extends StatefulWidget {
  final UserInformation userInfo;

  AppPage(this.userInfo);

  @override
  State<StatefulWidget> createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<AppPage>
    with SingleTickerProviderStateMixin {
//  SpUtil sp;
  TabController tabController;
  bool isSearch = false;


  List<Widget> _list = List();
  int _currentIndex = 0;
  List tabData = [
    {'text': '首页', 'icon': Icon(Icons.language)},
    {'text': '商务中心', 'icon': Icon(Icons.extension)},
    {'text': '生产中心', 'icon': Icon(Icons.import_contacts)},
    {'text': '智能中心', 'icon': Icon(Icons.account_circle)},
  ];
  List<BottomNavigationBarItem> _myTabs = [];
  String appBarTitle;

  @override
  void initState() {
    super.initState();
    this.tabController = TabController(length: 3, vsync: this);
    if(globals.userRole == "unknown")
      globals.userRole = "factory_owner";
    print("User role is ${globals.userRole}");
//    initSearchHistory();

    /*if(Application.pageIsOpen == true){// 是否展开业界动态
      tabData.insert(0, {'text': '综合看板', 'icon': Icon(Icons.language)});
      _list
      //..add(FirstPage())
        //..add(MainPage(userInfo: widget.userInfo));
//        ..add(FactoryOverview());
//          ..add(Factory2());
      ..add(Factory3());
    }*/
    appBarTitle = tabData[0]['text'];

    for (int i = 0; i < tabData.length; i++) {
      _myTabs.add(BottomNavigationBarItem(
        icon: tabData[i]['icon'],
        title: Text(
          tabData[i]['text'],
        ),
      ));
    }
    if(globals.userRole == "unknown")
      _list..add(DashBoard());   //Show change to Welcome, and ask to confirm Role
    else if(globals.userRole == 'factory_owner')
      _list..add(FactoryOwnerPage());
    else if(globals.userRole == 'operator_gov')
       _list..add(DashBoard());


    _list
      ..add(OrderList())
//        ..add(FourthPage())
      ..add(FactoryOverview())
      //..add(StickyDemo(title: 'Sliver - sticky效果'))
      ..add(AIPage(userInfo: widget.userInfo));
  }

  @override
  void dispose() {
    super.dispose();
  }


  /*Widget buildSearchInput(BuildContext context) {
    return new SearchInput((value) async {
      if (value != '') {
        print('value ::: $value');
        // List<WidgetPoint> list = await widgetControl.search(value);
        List<WidgetPoint> list = await DataUtils.searchWidget(value);
        return list
            .map((item) => new MaterialSearchResult<String>(
                  value: item.name,
                  icon: WidgetName2Icon.icons[item.name] ?? null,
                  text: 'widget',
                  onTap: () {
                    onWidgetTap(item, context);
                  },
                ))
            .toList();
      } else {
        return null;
      }
    }, (value) {}, () {});
  }*/

  renderAppBar(BuildContext context, Widget widget, int index) {
    if (index == 1 && Application.pageIsOpen == true) {
      return AppBar(
        //title: buildSearchInput(context),
        title: Text("已修改"),
        bottom: TabBar(
          controller: this.tabController,
          tabs: [
            Tab(icon: Icon(Icons.directions_car)),
            Tab(icon: Icon(Icons.directions_transit)),
            Tab(icon: Icon(Icons.directions_bike)),
          ],
        ),
      );
    } else if (index == 0 && Application.pageIsOpen == false) {
      //return AppBar(title: buildSearchInput(context));
      return AppBar(title: Text("已修改"));
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
     // appBar: renderAppBar(context, widget, _currentIndex),
      body: IndexedStack(
        index: _currentIndex,
        children: _list,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: _myTabs,
        //高亮  被点击高亮
        currentIndex: _currentIndex,
        //修改 页面
        onTap: _itemTapped,
        //shifting :按钮点击移动效果
        //fixed：固定
        type: BottomNavigationBarType.fixed,

        fixedColor: Theme.of(context).primaryColor,
      ),
    );
  }

  void _itemTapped(int index) {
    setState(() {
      _currentIndex = index;
      appBarTitle = tabData[index]['text'];
    });
  }
}
